package com.cloudwise.controller;




import com.cloudwise.entity.Business;
import com.cloudwise.entity.BusinessVo;
import com.cloudwise.entity.ReturnBean;
import com.cloudwise.service.BusinessService;
import com.cloudwise.service.BusinessVoService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Business)表控制层
 *
 * @author makejava
 * @since 2022-01-20 12:41:53
 */
@RestController
@RequestMapping("business")
public class BusinessController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessService businessService;
    @Resource
    private BusinessVoService businessVoService;


    @GetMapping("selectAll")
    public ReturnBean selectAll(Long page, Long limit,BusinessVo businessVo) {
        List<BusinessVo> businessVos = businessVoService.queryAll(page, limit,businessVo);
        Long count = businessVoService.getCount();
        return super.success(businessVos, "操作成功",count);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ReturnBean selectOne(@PathVariable Serializable id) {
        return super.success(this.businessService.getById(id),"操作成功");
    }

    /**
     * 新增数据
     *
     * @param business 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public ReturnBean insert(@RequestBody Business business) {
        return super.success(this.businessService.save(business),"操作成功");
    }

    /**
     * 修改数据
     *
     * @param business 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public ReturnBean update(@RequestBody Business business) {

        boolean update = this.businessService.updateById(business);
        if (update) {
            return super.success(business,"操作成功",1L);
        } else {
            return super.fail(business,"操作失败");
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public ReturnBean delete(@RequestParam(value = "idList[]",required = false) List<Long> idList) {
        boolean delete = this.businessService.removeByIds(idList);
        if (delete) {
            return super.success(null,"操作成功");
        } else {
            return super.fail(null,"操作失败");
        }
    }
    @RequestMapping("/getCount")
    public ReturnBean getCount(){
        int count = this.businessService.count();
        return success(null, "操作成功",(long) count);
    }
    @RequestMapping("/getAllDeptNoPage")
    public ReturnBean getAllDeptNoPage(){
        List<Business> list = businessService.list();
        return super.success(list,"操作成功", (long) list.size());
    }
}

